﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReAnimation.MVP.ViewInterfaces;
using ReAnimation.MVP.Models.Interfaces;
using ReAnimation.Helpers.IoC;
using ReAnimation.Helpers.WinForms;
using ReAnimation.Infrastructure.Services.Interfaces;
using ReAnimation.Domain;

namespace ReAnimation.MVP.Presenters
{
    public class AnimationStepSettingsPresenter: BasePresenter<IAnimationStepSettingsView>
    {
        private readonly IMainModel _mainModel;
        private readonly INotifier _notifier;
        private readonly ILazyInject<INewMoleculeDialogView> _newMoleculeDialogViewInj;
        private readonly ILazyInject<IOpenFileDialogView> _openFileDialogViewInj;
        private readonly IZMatrixService _zMatrixService;
        private readonly IOpenBabelService _openBabelService;
        //TODO: extract molecule loading functionality!!!
        public AnimationStepSettingsPresenter(IMainModel mainModel,
            ILazyInject<INewMoleculeDialogView> newMoleculeDialogViewInj,
            ILazyInject<IOpenFileDialogView> openFileDialogViewInj,
            INotifier notifier,
            IZMatrixService zMatrixService,
            IOpenBabelService openBabelService)
        {
            _openBabelService = openBabelService;
            _zMatrixService = zMatrixService;
            _notifier = notifier;
            _newMoleculeDialogViewInj = newMoleculeDialogViewInj;
            _openFileDialogViewInj = openFileDialogViewInj;
            _mainModel = mainModel;
            _mainModel.CurrentAnimationStepChanged += new EventHandler(_mainModel_CurrentAnimationStepChanged);
            _mainModel.CurrentMoleculeChanged += new EventHandler(_mainModel_CurrentMoleculeChanged);
        }

        void _mainModel_CurrentMoleculeChanged(object sender, EventArgs e)
        {
            UpdateViewEnabledState();
        }

        void _mainModel_CurrentAnimationStepChanged(object sender, EventArgs e)
        {
            UpdateViewEnabledState();
        }

        protected override void InitView()
        {
            UpdateViewEnabledState();
            View.NewMolecule += new EventHandler(View_NewMolecule);
            View.LoadMolecule += new EventHandler(View_LoadMolecule);
            View.DeleteMolecule += new EventHandler(View_DeleteMolecule);
        }

        void View_DeleteMolecule(object sender, EventArgs e)
        {
            if (_mainModel.CurrentMolecule != null)
            {
                _mainModel.CurrentAnimationStep.RemoveMolecule(_mainModel.CurrentMolecule);

                if (_mainModel.CurrentAnimationStep.Molecules.Count() > 0)
                    _mainModel.CurrentMolecule = _mainModel.CurrentAnimationStep.Molecules.First();
                else
                    _mainModel.CurrentMolecule = null;
            }
        }

        void View_LoadMolecule(object sender, EventArgs e)
        {
            IOpenFileDialogView openFileDialogView = _openFileDialogViewInj.Get();
            openFileDialogView.Title = "Load molecule";//TODO: extract

            if (openFileDialogView.ShowOpenFileDialog())
            {
                String fileName = openFileDialogView.FileName;

                ZMatrix zMatrix = _openBabelService.OpenZMatrixFromFile(fileName);
                if (zMatrix == null)
                    _notifier.ShowMessage("Error", "Can't read zMatrix");
                else
                {
                    _zMatrixService.ConvertZMatrixToCartesian(zMatrix, _mainModel.CurrentMolecule);
                    _mainModel.CurrentMolecule.MoleculeZMatrix = zMatrix;

                    _mainModel.CurrentMoleculeZMatrixWasModified();
                    _mainModel.CurrentMoleculeStructureWasModified();
                }
            }
        }

        void View_NewMolecule(object sender, EventArgs e)
        {
            _newMoleculeDialogViewInj.Get().ShowViewDialog();
        }

        private void UpdateViewEnabledState()
        {
           View.MoleculeDetailsEnabled =
           View.LoadEnabled =
           View.DelEnabled = _mainModel.CurrentMolecule != null;
           View.NewEnabled = _mainModel.CurrentAnimationStep != null;
        }
    }
}
